<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>PDF Extractor</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f5f5f5;
        }
        
        .pdf-container {
            display: flex;
            flex-direction: row;
            height: calc(100vh - 150px);
            margin-top: 20px;
        }
        
        .pdf-viewer {
            flex: 1;
            padding: 20px;
            background: white;
            margin-right: 10px;
            overflow: auto;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
        }
        
        .text-viewer {
            flex: 1;
            padding: 20px;
            background: white;
            margin-left: 10px;
            overflow: auto;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
            white-space: pre-wrap;
        }
        .loading-mask {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0, 0, 0, 0.5);
            display: flex;
            justify-content: center;
            align-items: center;
            z-index: 1000;
            display: none;
        }
        
        .loading-spinner {
            width: 50px;
            height: 50px;
            border: 5px solid #f3f3f3;
            border-top: 5px solid #4CAF50;
            border-radius: 50%;
            animation: spin 1s linear infinite;
        }
        
        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
        header {
            background-color: #4CAF50;
            color: white;
            padding: 1rem;
            text-align: center;
        }
        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 2rem;
        }
        .upload-area {
            border: 2px dashed #ccc;
            padding: 2rem;
            text-align: center;
            margin-bottom: 2rem;
            background-color: white;
        }
        button {
            background-color: #4CAF50;
            color: white;
            border: none;
            padding: 0.5rem 1rem;
            cursor: pointer;
            font-size: 1rem;
        }
        button:hover {
            background-color: #45a049;
        }
        #result {
            margin-top: 2rem;
            padding: 1rem;
            background-color: white;
            border: 1px solid #ddd;
        }
    </style>
</head>
<body>
    <header>
        <h1>PDF Text Extractor</h1>
        <p>Upload PDF files to extract text using AI</p>
    </header>

    <div class="loading-mask">
        <div class="loading-spinner"></div>
    </div>
    
    <div class="container">
        <div class="upload-area">
            <h2>Upload PDF File</h2>
            <input type="file" id="pdfUpload" accept=".pdf">
            <button id="uploadBtn">Process PDF</button>
        </div>

        <div class="pdf-container">
            <div class="pdf-viewer">
                <iframe id="pdfPreview" style="width:100%; height:100%; border:none;"></iframe>
            </div>
            <div class="text-viewer" id="extractedText"></div>
        </div>
    </div>

    <script>
        document.getElementById('uploadBtn').addEventListener('click', function() {
            const fileInput = document.getElementById('pdfUpload');
            if (fileInput.files.length === 0) {
                alert('Please select a PDF file first');
                return;
            }
            
            // 显示加载蒙版
            document.querySelector('.loading-mask').style.display = 'flex';
            
            const file = fileInput.files[0];
            const formData = new FormData();
            formData.append('file', file);
            
            fetch('/api/extract-pdf', {
                method: 'POST',
                body: formData
            })
            .then(response => response.json())
            .then(data => {
                document.getElementById('extractedText').innerText = data.text;
                document.getElementById('pdfPreview').src = `/static/uploads/${file.name}`;
            })
            .finally(() => {
                // 隐藏加载蒙版
                document.querySelector('.loading-mask').style.display = 'none';
            })
            .catch(error => {
                console.error('Error:', error);
                document.querySelector('.loading-mask').style.display = 'none';
                alert('Error processing PDF');
            });
        });
    </script>
</body>
</html>